package Object;

import java.util.HashSet;
import java.util.Map;
import java.util.Objects;

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        // 检查是否是同一个对象的引用
        if(this == obj) return true;
        // 检查是否是同一个类型
        if(obj == null || getClass() != obj.getClass()) return false;
        // 类型转换后比较字段值
        Person person = (Person) obj;
        return age == person.age && Objects.equals(name, person.name);
    }

    // 使用Objects类的hash方法生成哈希码，需要传入所有参与equals比较的字段
//    @Override
//    public int hashCode() {
//        return Objects.hash(name, age);
//    }

    public static void main(String[] args) {
        Person person = new Person("hahha", 14);
        System.out.println(person.hashCode());
        System.out.println(System.identityHashCode(person));
        Person person1 = new Person("hahha", 14);
        System.out.println(person1.hashCode());
        System.out.println(System.identityHashCode(person1));
        System.out.println(person1.equals(person));
        HashSet<Person> set = new HashSet<>();
        set.add(person);
        set.add(person1);
        for (Person per: set) {
            System.out.println(per);
        }
    }
}
